﻿;


//弹出插件 初始化 [icon:0 感叹号 1 打钩 2 打叉 3问号 4锁图像 5不开心头像 6开心头像 7 下载图标]
layer.config({
    extend: ["extend/layer.ext.js", "skin/moon/style.css"],
    skin: "layer-ext-moon"
});

//全局对象
var BootStrapUI_Core = {
    keyCode: {
        ENTER: 13, ESC: 27, END: 35, HOME: 36,
        SHIFT: 16, TAB: 9,
        LEFT: 37, RIGHT: 39, UP: 38, DOWN: 40,
        DELETE: 46, BACKSPACE: 8
    },
    statusCode: { ok: 200, error: 300, timeout: 301 },
    ajaxDone: function (json) {
        if (json.statusCode == BootStrapUI_Core.statusCode.error) {
            if (json.message) parent.layer.msg(json.message, { icon: 2 });
        } else if (json.statusCode == BootStrapUI_Core.statusCode.timeout) {
            if (json.message) {
                parent.layer.alert(json.message, { icon: 2 }, function () {
                    window.top.location.href = json.redirectUrl;
                });
            } else {
                window.top.location.href = json.redirectUrl;
            }
        } else {
            if (json.message) parent.layer.msg(json.message, { icon: 1 });
        };
    },
    ajaxError: function (xhr, ajaxOptions, thrownError) {
        alertError('操作异常');
        return;
    },
    alertError: function (msg) {
        layer.alert(msg, { icon: 2 });
    },
    alert: function (msg) {
        layer.alert(msg, { icon: 1 });
    }
};

//将表单内容转换成对象
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function queryParams(params) {
    var defaultOptions = {
        rows: params.pageSize,
        page: params.pageNumber,
        sort: params.sortName || 'Id',
        order: params.sortOrder
    };
    var options = $('#searchForm').serializeObject();
    var op = $.extend({}, defaultOptions, options);
    return op;
}

//表格方法
function btTable(id, options) {
    var defaultOptions = {
        method: 'get',
        //height: $(window).height() - 105,
        pageList:[10,20,50,100],
        striped: true,
        cache: false,
        dataType: "json",
        pagination: true,
        queryParamsType: "",
        contentType: "application/x-www-form-urlencoded",
        pageSize: 10,
        search: false,
        showColumns: false,
        sidePagination: "server",
        queryParams: queryParams,
        clickToSelect: true,
        singleSelect: true,
        sortOrder: 'desc',
        silentSort:false,
        showColumns: true,
        sortable:true,
        classes: 'table table-hover table-condensed table-striped table-bordered'
    };
    var op = $.extend({}, defaultOptions, options);
    $(id).bootstrapTable(op);
}

//初始化界面插件
function initUI() {
    //初始化日期控件
    if ($.fn.datepicker) {
        $('.datepicker').datepicker({
            format: 'yyyy-mm-dd',
            language: 'zh-CN',
            autoclose: true,
            clearBtn: false,
            todayBtn: 'linked',
            todayHighlight: true
        });
    }

    //初始化下拉菜单 (因为使用了插件，所以validate的方法需要在change里面手动触发,并需要修改微软jquery.validate.unobtrusive.js源代码,加上[$.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" })])
    //search_contains: true [支持模糊查询]
    if ($.fn.chosen) {
        $('.chosen-select').chosen({
            no_results_text: '没有找到结果',
            search_contains: true
        }).change(function () {
            var ID = $(this).attr("id");
            if (!$(this).valid()) {
                $("#" + ID + "_chzn a").addClass("input-validation-error");
            }
            else {
                $("#" + ID + "_chzn a").removeClass("input-validation-error");
            }
        });
    }

    var $switcheryList = $('.js-switch');
    for (var i = 0, len = $switcheryList.size(); i < len; i++) {
        var switchItem = $switcheryList.get(i);
        new Switchery(switchItem, { size: 'small' });
    }
}

$(function () {
    initUI();
})

